package o;

import android.net.ConnectivityManager;
import android.os.Handler;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.downloadengine.impl.DownloadService;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class bnv implements Runnable, bnb {
    private c ʻ;
    private long ʼ;
    private boolean ʽ = true;
    private Handler ˊ = bnl.ˏ().ॱॱ();
    private int ˋ = 0;
    private bnw ˎ = null;
    private SessionDownloadTask ˏ;
    private volatile boolean ॱ = true;
    private long ᐝ;

    class c {
        private Timer ʻ = new Timer();
        private boolean ʼ = false;
        private TimerTask ʽ = ॱ();
        private volatile long ˊ = 0;
        private volatile int ˋ = 0;
        private volatile long ˎ = 0;
        private int ˏ = 0;
        final /* synthetic */ bnv ॱ;
        private long ॱॱ = 1500;

        public c(bnv o_bnv) {
            this.ॱ = o_bnv;
        }

        private TimerTask ॱ() {
            return new TimerTask(this) {
                final /* synthetic */ c ˊ;

                {
                    this.ˊ = r1;
                }

                public void run() {
                    try {
                        this.ˊ.ˋ(true, false);
                    } catch (RuntimeException e) {
                        bnc.ˎ.ˏ("HiAppDownload", "progress timerTask exception:" + e.getMessage());
                    }
                }
            };
        }

        public void ˏ(boolean z) {
            ˋ(false, z);
        }

        public void ˏ() {
            if (this.ʻ != null) {
                this.ॱ.ˏ(this.ʻ);
            }
        }

        public void ˎ() {
            this.ॱ.ˎ(this.ʻ, this.ʽ);
        }

        private void ˋ(boolean z, boolean z2) {
            if (this.ॱ.ˏ.ˈ() == 2) {
                long currentTimeMillis = System.currentTimeMillis();
                if (z && this.ʼ) {
                    if (currentTimeMillis - this.ˎ >= 5000) {
                        z2 = true;
                    } else {
                        return;
                    }
                }
                long j = 0;
                long j2 = 0;
                for (SplitTask splitTask : this.ॱ.ˏ.ᐝˊ()) {
                    j2 += splitTask.ˈ();
                    j = splitTask.ʼॱ() + j;
                }
                this.ˏ = (int) (((((double) j2) * 1.0d) / ((double) j)) * 100.0d);
                if (!z2) {
                    if (this.ˏ == this.ˋ) {
                        return;
                    }
                    if (currentTimeMillis - this.ˎ < this.ॱॱ && this.ˏ - this.ˋ < 10) {
                        return;
                    }
                }
                int i = (int) (((double) (j2 - this.ˊ)) / (((double) (currentTimeMillis - this.ˎ)) / 1000.0d));
                SessionDownloadTask ॱ = this.ॱ.ˏ;
                if (i < 0) {
                    i = 0;
                }
                ॱ.ˏॱ(i);
                this.ॱ.ˏ.ʽ(this.ˏ > 100 ? 100 : this.ˏ);
                this.ॱ.ˏ.ˏ(j2);
                this.ॱ.ˏ.ॱ(j);
                this.ॱ.ॱˊ();
                if (!z) {
                    this.ʼ = true;
                }
                this.ˎ = currentTimeMillis;
                this.ˊ = j2;
                this.ˋ = this.ˏ;
                this.ॱ.ˊॱ();
            }
        }
    }

    public bnv(SessionDownloadTask sessionDownloadTask) {
        this.ˏ = sessionDownloadTask;
        this.ʻ = new c(this);
    }

    private void ˏ() {
        long j = 0;
        for (SplitTask splitTask : this.ˏ.ᐝˊ()) {
            if (splitTask.ʾ() <= 0) {
                if (!ˋ(splitTask) || this.ˏ.ꜟ()) {
                    splitTask.ॱ(2);
                } else {
                    splitTask.ॱ(1);
                }
                ˏ(splitTask);
            }
            j = splitTask.ʼॱ() + j;
        }
        this.ˏ.ॱ(j);
    }

    private void ˏ(SplitTask splitTask) {
        if (splitTask.ʾ() == 1) {
            splitTask.ˏॱ(splitTask.ʽ());
            splitTask.ˎ(splitTask.ʻ());
            splitTask.ˋॱ(splitTask.ˏॱ());
            return;
        }
        splitTask.ˏॱ(splitTask.ᐝ());
        splitTask.ˎ(splitTask.ʼ());
        splitTask.ˋॱ(splitTask.ॱॱ());
    }

    private boolean ˋ(SplitTask splitTask) {
        return !TextUtils.isEmpty(splitTask.ʽ()) && splitTask.ʻ() > 0;
    }

    public void run() {
        bnc.ˎ.ˋ("HiAppDownload", "begin SessionTaskDownloader ,package:" + this.ˏ.ˊˊ());
        this.ʼ = System.currentTimeMillis();
        try {
            ॱॱ();
            ˊ();
            ʽ();
            ˏ();
            ˋ(2);
            this.ʻ.ˎ();
            for (SplitTask splitTask : this.ˏ.ᐝˊ()) {
                ͺ();
                if (splitTask.ˊᐝ() == 0) {
                    this.ʽ = false;
                    bnc.ˎ.ˋ("HiAppDownload", "splitTask already done, id = " + splitTask.ˊˋ());
                } else {
                    this.ˎ = new bnw(this, splitTask);
                    this.ˎ.ॱॱ();
                    this.ˎ.ʼ().ˏ(0);
                    boolean z = this.ʽ && this.ˎ.ʻ();
                    this.ʽ = z;
                }
            }
            ʼ();
        } catch (bnj e) {
            if (!(this.ˎ == null || this.ˎ.ʼ() == null)) {
                this.ˎ.ʼ().ˏ(-1);
            }
            ॱ(e);
        }
        ˏॱ();
    }

    private void ॱ() {
        Map hashMap = new HashMap();
        hashMap.put("errorCode", String.valueOf(201));
        if (this.ˎ == null || this.ˎ.ʼ().ˊᐝ() != 0) {
            hashMap.put("downloadResult", "1");
            hashMap.put("lastErrorCode", String.valueOf(this.ˏ.ˏॱ().ˋ));
        } else {
            hashMap.put("downloadResult", "0");
        }
        int ˈ = this.ˏ.ˈ();
        if (ˈ == 6) {
            hashMap.put("downloadResult", "2");
        }
        if (ˈ == 3) {
            hashMap.put("downloadResult", "3");
        }
        hashMap.put("backupDomainInfo", bnl.ˏ().ͺ());
        if (this.ˎ != null) {
            bni.ˊ(hashMap, this.ˏ, this.ˎ.ʼ());
        } else {
            bni.ˊ(hashMap, this.ˏ, null);
        }
    }

    private void ˊ() {
        ConnectivityManager connectivityManager = (ConnectivityManager) bnl.ˏ().ॱ().getSystemService("connectivity");
        if (connectivityManager != null) {
            this.ˏ.ͺ().ˋ(connectivityManager.getActiveNetworkInfo());
        }
    }

    private void ॱॱ() throws bnj {
        String str;
        Object obj = null;
        if (this.ˏ.ᐝˊ().isEmpty()) {
            str = "sessionDownloadTask.splitTaskList is empty";
        } else if (this.ˊ == null) {
            str = "no handler!";
        } else {
            int i = 1;
            str = null;
        }
        if (obj == null) {
            throw new bnj(100, str);
        } else if (bny.ˊ()) {
            bns.ॱ().ˊ();
        }
    }

    private void ʽ() {
        this.ˏ.ˏॱ(0);
        ˋ(1);
        ॱˊ();
    }

    private void ʼ() {
        ˋ(4);
        this.ᐝ = System.currentTimeMillis();
        this.ˏ.ͺ().ॱ(this.ʼ);
        this.ˏ.ͺ().ˎ(this.ᐝ);
        this.ˏ.ͺ().ˎ(!this.ʽ);
        bnc.ˎ.ˋ("HiAppDownload", "sessionTaskDownloader result:succeed ,  package:" + this.ˏ.ˊˊ());
    }

    private void ॱ(bnj o_bnj) {
        bnc.ˎ.ˊ("HiAppDownload", "sessionTaskDownloader result:failed, errorCode:" + o_bnj.ˊ() + ", errorMessage:" + o_bnj.ˏ() + ",  package:" + this.ˏ.ˊˊ());
        this.ˏ.ˏॱ().ˋ = o_bnj.ˊ();
        this.ˏ.ˏॱ().ˎ = o_bnj.ˏ();
        ˋ(5);
        this.ᐝ = System.currentTimeMillis();
        this.ˏ.ͺ().ॱ(this.ʼ);
        this.ˏ.ͺ().ˎ(this.ᐝ);
        this.ˏ.ͺ().ˎ(!this.ʽ);
        if (o_bnj.ˊ() == 121) {
            bnl.ˏ().ʻ().ˏ(this.ˏ, bnl.ˏ().ʻ().ˏ(this.ˏ));
        }
        if (this.ॱ) {
            if (bno.ॱ(o_bnj)) {
                bni.ˊ(o_bnj.ˋ(), this.ˏ, null);
            }
            ᐝ();
            return;
        }
        ˎ(o_bnj);
    }

    private void ʻ() {
        bny.ˋ(this.ˏ);
    }

    private void ˎ(bnj o_bnj) {
        if (this.ˋ == 3) {
            ˋ(3);
        } else {
            ˋ(6);
        }
        int ˊ = o_bnj.ˊ();
        switch (this.ˋ) {
            case 1:
                ˊ = 105;
                break;
            case 2:
                ˊ = 129;
                break;
            case 3:
                ˊ = 104;
                break;
            case 5:
            case 6:
                ˊ = 121;
                break;
        }
        o_bnj.ˊ(ˊ);
        o_bnj.ˋ(o_bnj.ˏ() + ", interrupt reason:" + this.ˋ);
        bnc.ˎ.ˋ("HiAppDownload", "update errorCode according to interrupt reason:" + ˊ);
        this.ˏ.ˏॱ().ˋ = ˊ;
        bni.ˊ(o_bnj.ˋ(), this.ˏ, null);
    }

    private void ᐝ() {
        int i = this.ˏ.ˏॱ().ˋ;
        if (i == 129) {
            this.ˏ.ॱ(true, 2);
            ˋ(6);
        } else if (this.ˏ.ʼॱ() != 0 && this.ˏ.ˏˏ()) {
            if (bno.ˎ(i) || i == 119 || i == 121) {
                ˋ(6);
            }
        }
    }

    private void ˏॱ() {
        if (this.ˏ.ˈ() == 6) {
            ˊॱ();
        } else if (this.ˏ.ˈ() == 3 || this.ˏ.ˈ() == 5) {
            ʻ();
        } else if (this.ˏ.ˈ() == 4) {
            bnl.ˏ().ॱ(this.ˏ);
            bnt.ˋ().ॱ(this.ˏ);
        }
        ॱ();
        this.ˏ.ॱ(false);
        ॱˊ();
        this.ʻ.ˏ();
        DownloadService.ˊ();
    }

    private void ॱˊ() {
        if (this.ॱ) {
            bny.ˏ(this.ˏ);
        }
    }

    public void ˏ(int i) {
        bnc.ˎ.ˋ("HiAppDownload", "stop sessionTaskDownloader");
        this.ॱ = false;
        this.ˋ = i;
        this.ˏ.ॱ(true, i);
        if (this.ˎ != null) {
            this.ˎ.ʽ();
        }
    }

    private void ˋ(int i) {
        if (this.ॱ) {
            this.ˏ.ॱॱ(i);
        }
    }

    private void ͺ() throws bnj {
        if (!this.ॱ) {
            throw new bnj(DnsConfig.MAX_CACHE_ENTRIES, "download interrupted : " + this.ˋ);
        }
    }

    SessionDownloadTask ˋ() {
        return this.ˏ;
    }

    boolean ˎ() {
        return this.ॱ;
    }

    void ॱ(boolean z) {
        if (this.ॱ) {
            this.ʻ.ˏ(z);
        }
    }

    private void ˊॱ() {
        bnt.ˋ().ˊ(this.ˏ);
    }

    private void ˎ(Timer timer, TimerTask timerTask) {
        if (timer != null && timerTask != null) {
            try {
                timer.schedule(timerTask, 0, 5000);
            } catch (RuntimeException e) {
                bnc.ˎ.ˏ("HiAppDownload", "schedule task:" + e.getMessage());
            }
        }
    }

    private void ˏ(Timer timer) {
        if (timer != null) {
            try {
                timer.cancel();
            } catch (RuntimeException e) {
                bnc.ˎ.ˏ("HiAppDownload", "cancelTimer:" + e.getMessage());
            }
        }
    }
}
